//模型层 获取数据
import {
  HTTP
} from '../utils/http';

//继承使用HTTP工具里的ajax方法
class IndexModel extends HTTP {
  //获取商品列表信息 需要tpl模板
  getGoodsList(tpl) {
    //利用Promise里resolve写好的字符串和数据结合一起抛出到另外的模块
    return new Promise((resolve, reject) => {
      this.ajax({
        url: 'Shopping_cart/getGoodsList',
        type: 'POST',
        dataType: 'JSON',
        success: (data) => {
          let list = '';
          data.forEach((elem) => {
            list += tpl().replace(/{{(.*?)}}/g, (node, key) => {
              return {
                id: elem.id,
                img_url: elem.img_url,
                goods_name: elem.goods_name,
                price: elem.price
              } [key];
            })
          })
          //利用resolve方法把list放出去
          resolve(list);
        }
      })
    })
  }
}

//导出模块
export {
  IndexModel
};